-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[MXNET-1382] Add the index_array operator #14638
Conversation
Thanks for your contributions @nickguletskii. Can you also look into the CI failures ? |
@mxnet-label-bot Update [Operator, pr-awaiting-review] |
The CI failure seems to be unrelated. Here's the relevant part of the log:
If I am not mistaken, this error code corresponds to the following error message: "CUDA driver version is insufficient for CUDA runtime version". Could this be caused by the recent update to the |
Thanks for implementing this! This seems similar to what I requested in #14574, correct? Any reason this should be in the contrib namespace? |
@fhieber In a way, yes. The behaviour you have described in #14574 can be emulated by applying There's no particular reason why I've placed |
Thanks @nickguletskii , I agree, your operator seems more versatile, thanks for working on it!
|
@piyushghai Sorry to bother you, but should I rebase this pull request on master now that the failing job (test_unix_python3_tensorrt_gpu) has been disabled (#14642), or should I just leave it as it is and wait for the review? |
@nickguletskii You should rebase with the latest master branch. That'll enable you to push again, allowing the CI to pass on your PR. |
4ea3cf6
to
0f742af
Compare
7fa5a17
to
aba94af
Compare
Huh, this is weird, I've updated this PR to be compatible with the recently merged support for zero-dim and zero-shape arrays, and now the CI for Windows CPU tests are failing. However, when I try to run the tests locally (on a 64 bit Windows machine), the tests pass just fine. I'll make further attempts to reproduce this issue on my local machine tomorrow. I suspect that I've screwed up with memory access somewhere because the test runner crashes without providing any useful debugging information. |
The previous CI failures on Windows were caused by indexing C-style arrays using ints. I'm not sure whether this was just undefined behaviour or a bug in the older versions of MSVC++, but it did not happen with VC++ 16.0. I fixed the issue by using ptrdiff_t to index C-style arrays. |
@szha @reminisce This PR should be ready for a review now. The CI failure seems to be unrelated (the failing test is test_operator_gpu.test_convolution_multiple_streams, described in #14329). |
@szha @reminisce @fhieber Is this PR good to go? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution and sorry for the late review. Have some comments on code cosmetics.
@@ -2130,6 +2130,68 @@ def test_bilinear_sampler_versions(): | |||
if req_dict['grid'] is 'write': | |||
assert_almost_equal(exe.grad_dict['grid'].asnumpy(), exe_list[ref_idx].grad_dict['grid'].asnumpy(), rtol=1e-3, atol=1e-5) | |||
|
|||
@with_seed() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests defined in test_operator.py
will be run with ctx=mx.gpu()
in CI. There is no need to duplicate test code in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I've removed these redundant tests in 3cdfcf5.
|
||
@with_seed() | ||
def test_index_array_default_zero_dim(): | ||
with mx.np_compat(active=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the whole function is expected to be numpy compatible, you can use a decorator mx.use_np_compat
to wrap the whole function. It would be easier for us to just delete the decorator in the next major release than putting additional efforts of adjusting indentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I've updated the subtests to use @mx.use_np_compat
in 139567b.
|
||
@with_seed() | ||
def test_index_array_default_zero_size(): | ||
with mx.np_compat(active=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above, please use the decorator mx.use_np_compat
.
|
||
@with_seed() | ||
def test_index_array_select_axes_zero_size(): | ||
with mx.np_compat(active=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
@nickguletskii Could you please re-trigger the CI again? |
Changes the implementation of index_array to be compatible with the recently merged support for zero-dim and zero-size arrays. Resolves the incompatibilities with apache#14661.
In the previous implementation, the output gradient had an incorrect shape. This commit fixes the shapes and makes the tests more readable.
Solves access violations when compiling with MSVC++ 14.0.
89e9cf5
to
69a9969
Compare
* Implement the index_array operator * Add index_array operator tests * Add index_array operator GPU tests * Add the index_array operator to the Python docs autosummary * Add the author of the index_array operator to CONTRIBUTORS.md * Make index_array compatible with zero-dim and zero-size arrays Changes the implementation of index_array to be compatible with the recently merged support for zero-dim and zero-size arrays. Resolves the incompatibilities with apache#14661. * Fix the index_array gradient checks in the unit tests In the previous implementation, the output gradient had an incorrect shape. This commit fixes the shapes and makes the tests more readable. * Add zero-dim and zero-size array tests for index_array * Use mxnet::Tuple<int> instead of TShape for the axes parameter * Fix incorrect array indexing in index_array Solves access violations when compiling with MSVC++ 14.0. * Avoid copying the input shape array in the index_array shape function * Add unknown shape handling to index_array * Use SHAPE_ASSIGN_CHECK to assign the shape in index_array * Remove the redundant index_array GPU tests from test_operator_gpu.py * Move the index_array tests into a single function (test_index_array) * Use @mx.use_np_compat instead of mx.np_compat in index_array op tests * Remove the use of template specialization for IndexArrayForward * Add the index_array operator to the AMP symbol list * Retrigger CI
Description
This pull request implements
index_array
, an operator that returns an array of indexes of the input array.For an input array with shape
(d_1, d_2, ..., d_n)
,index_array
returns a(d_1, d_2, ..., d_n, n)
arrayidx
, whereidx[i_1, i_2, ..., i_n, :] = [i_1, i_2, ..., i_n]
.Additionally, when the parameter
axes
is specified,idx
will be a(d_1, d_2, ..., d_n, m)
array wherem
is the length ofaxes
, and the followingequality will hold:
idx[i_1, i_2, ..., i_n, j] = i_{axes[j]}
.Examples:
Motivation
This operator can be used to generate meshgrids for tensors without knowing their exact shapes during construction. For instance, this operator can be used to make a makeshift prior box generator for anchor-based computer vision models:
Also, this operator can be applied to implement positional encodings for sequence processing, e.g.:
I've also encountered situations where this operator would have been useful for some indexing tricks.
Checklist
Essentials
Please feel free to remove inapplicable items for your PR.
Changes
Comments
test_operator_gpu.py
are exactly the same as intest_operator.py
. I couldn't find any evidence thattest_operator.py
ever gets called with a GPUdefault_context
, so I copied the tests intotest_operator_gpu.py
to make sure that the GPU implementation works too.